﻿using PenguinForce.Entities;
using Microsoft.Xna.Framework;

namespace PenguinForce.FlockBehaviors
{
    /// <summary>
    /// SeekBehavior is a Behavior that makes an animal seek out another animal.
    /// </summary>
    class SeekBehavior : Behavior
    {
        #region Initialization
        public SeekBehavior(Entity animal)
            : base(animal)
        {
        }
        #endregion

        #region Update

        /// <summary>
        /// CohesionBehavior.Update infuences the owning animal to move towards the
        /// otherAnimal that it sees as long as it isn’t too close, in this case 
        /// that means inside the separationDist in the passed in AIParameters.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Entity otherAnimal, AIParameters aiParams)
        {
            //base.ResetReaction();

            //Vector2 pullDirection = Vector2.Zero;
            //float weight = int.MaxValue; // aiParams.perMemberWeight;

            ////if the otherAnimal is too close we dont' want to fly any
            ////closer to it
            //if (true) //Animal.ReactionDistance > 0.0f && Animal.ReactionDistance > aiParams.separationDist)
            //{
            //    //We want to make the animal move closer the the otherAnimal so we 
            //    //create a pullDirection vector pointing to the otherAnimal bird and 
            //    //weigh it based on how close the otherAnimal is relative to the 
            //    //AIParameters.separationDistance.
            //    pullDirection = -(Animal.Location - Animal.ReactionLocation);
            //    Vector2.Normalize(ref pullDirection, out pullDirection);

            //    //weight *= (float)Math.Pow((double)(Animal.ReactionDistance - aiParams.separationDist) / (aiParams.detectionDist - aiParams.separationDist), 32);

            //    pullDirection *= weight;

            //    reacted = true;
            //    reaction = pullDirection;
            //}

            base.ResetReaction();

            Vector2 dangerDirection = Vector2.Zero;

            //Vector2.Dot will return a negative result in this case if the 
            //otherAnimal is behind the animal, in that case we don’t have to 
            //worry about it because we’re already moving away from it.
            //if (Vector2.Dot(Animal.Location, Animal.ReactionLocation) >= -(Math.PI / 2))
            //{
            //set the animal to fleeing so that it flashes red
            Animal.Fleeing = true;
            reacted = true;

            dangerDirection = -1 * (Animal.Location - Animal.ReactionLocation);
            Vector2.Normalize(ref dangerDirection, out dangerDirection);

            reaction = (aiParams.perDangerWeight * dangerDirection);
            //}

        }
        #endregion
    }
}